﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using DevExpress.Data.PivotGrid;
using DevExpress.Utils;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraCharts;
using Capa_Entidad;
using Negocio.Clases_BL;
using System.Web.UI;
//"[Categories].[Category Name].[Category Name]";

public partial class wfSegmentarPaciente : System.Web.UI.Page
{

    private static String CadenaReporte = "";
    protected void Page_Load(object sender, EventArgs e)

    {
       // Cargar_Cubo();
        //Totales(dgCuboAdmPaciente.Fields["CIT_ID_CITA"].CustomTotals);
    }
    protected void ShowRowSubTotals_CheckedChanged(object sender, EventArgs e)
    {

        if (ShowRowSubTotals.Checked)
        {
            dgCuboAdmPaciente.OptionsView.ShowRowTotals = true;
        }
        else
        {
            dgCuboAdmPaciente.OptionsView.ShowRowTotals = false;
        }
        
    }
    protected void ShowColumnSubTotals_CheckedChanged(object sender, EventArgs e)
    {
        if (ShowColumnSubTotals.Checked)
        {
            dgCuboAdmPaciente.OptionsView.ShowColumnTotals = true;
        }
        else
        {
            dgCuboAdmPaciente.OptionsView.ShowColumnTotals = false;
        }
    }

   
        

    void Cargar_Cubo()
    {
        //clsPacienteBLcs CuboAdm = new clsPacienteBLcs();
        //dgCuboAdmPaciente.DataSource = CuboAdm.Cubo_Adminsion();
        //dgCuboAdmPaciente.DataBind();
        Negocio.cnDB Conex = new Negocio.cnDB();
        SqlConnection cn = new SqlConnection();
        Conex.ConexBD(cn);
       DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter("select * from  V_Cubo_Admision", cn);
        SqlCommandBuilder cmdBldr = new SqlCommandBuilder(da);
        da.Fill(ds, "V_Cubo_Admision");
        dgCuboAdmPaciente.DataSource = ds;
        dgCuboAdmPaciente.DataMember = "V_Cubo_Admision";
        dgCuboAdmPaciente.DataBind();
       

   
    }


    protected void btnVerInforme_Click(object sender, EventArgs e)
    {
       // pvExport.ExportHtmlToResponse();
        
    }
    protected void buttonOpen_Click(object sender, EventArgs e)
    {
        Export(true);

       
    }
    void Export(bool saveAs)
    {
     
        pvExport.OptionsPrint.PrintHeadersOnEveryPage = checkPrintHeadersOnEveryPage.Checked;
        pvExport.OptionsPrint.PrintFilterHeaders = checkPrintFilterHeaders.Checked ? DefaultBoolean.True : DefaultBoolean.False;
        pvExport.OptionsPrint.PrintColumnHeaders = checkPrintColumnHeaders.Checked ? DefaultBoolean.True : DefaultBoolean.False;
        pvExport.OptionsPrint.PrintRowHeaders = checkPrintRowHeaders.Checked ? DefaultBoolean.True : DefaultBoolean.False;
        pvExport.OptionsPrint.PrintDataHeaders = checkPrintDataHeaders.Checked ? DefaultBoolean.True : DefaultBoolean.False;

        string fileName = "PivotGrid";
        switch (listExportFormat.SelectedIndex)
        {
            case 0:
                pvExport.ExportPdfToResponse(fileName, saveAs);
                break;
            case 1:
                pvExport.ExportXlsToResponse(fileName, saveAs);
                break;
            case 2:
                pvExport.ExportMhtToResponse(fileName, "utf-8", "ASPxPivotGrid Printing Sample", true, saveAs);
                break;
            case 3:
                pvExport.ExportRtfToResponse(fileName, saveAs);
                break;
            case 4:
                pvExport.ExportTextToResponse(fileName, saveAs);
                break;
            case 5:
                pvExport.ExportHtmlToResponse(fileName, "utf-8", "ASPxPivotGrid Printing Sample", true, saveAs);
                break;
        }
    }
   

    protected void buttonSaveAs_Click(object sender, EventArgs e)
    {
        Export(false);
    }

    //void Totales(DevExpress.Web.ASPxPivotGrid.PivotGridCustomTotalCollection totals)
    //{
    //    totals.Clear();
    //    if (chkPronedio.Checked == true)
    //        totals.Add(PivotSummaryType.Average);
    //    if (chkCuenta.Checked == true)
    //        totals.Add(PivotSummaryType.Count);
    //    //if (max.Checked == true)
    //    //    totals.Add(PivotSummaryType.Max);
    //    //if (min.Checked == true)
    //    //    totals.Add(PivotSummaryType.Min);
    //    if (chkDesEst.Checked == true)
    //        totals.Add(PivotSummaryType.StdDev);
    //    //if (stdevp.Checked == true)
    //    //    totals.Add(PivotSummaryType.StdDevp);
    //    if (chkSuma.Checked == true)
    //        totals.Add(PivotSummaryType.Sum);
    //    //if (var.Checked == true)
    //    //    totals.Add(PivotSummaryType.Var);
    //    //if (varp.Checked == true)
    //    //    totals.Add(PivotSummaryType.Varp);
    //}


    protected void btnVerGrafico_Click(object sender, EventArgs e)
    {
      //String  NomCampo   = txtNomCampo.Text;

      //PivotGridField unboundField = dgCuboAdmPaciente.Fields.Add(NomCampo, PivotArea.DataArea);

      //  unboundField.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
      //  dgCuboAdmPaciente.ShowUnboundExpressionEditor(unboundField);

        ViewType[] restrictedTypes = new ViewType[] { ViewType.PolarArea, ViewType.PolarLine, ViewType.SideBySideGantt,
				ViewType.SideBySideRangeBar, ViewType.RangeBar, ViewType.Gantt, ViewType.PolarPoint, ViewType.Stock, ViewType.CandleStick,
				ViewType.Bubble };
        foreach (ViewType type in Enum.GetValues(typeof(ViewType)))
        {
            if (Array.IndexOf<ViewType>(restrictedTypes, type) >= 0) continue;
            ChartType.Items.Add(type.ToString());
        }
        ChartType.SelectedItem = ChartType.Items.FindByText(ViewType.Line.ToString());
        SetChartType(ChartType.SelectedItem.Text);
        PointLabels.Checked = WebChartControl1.SeriesTemplate.Label.Visible;
        dgCuboAdmPaciente.OptionsView.ShowFilterHeaders = false;
        dgCuboAdmPaciente.OptionsChartDataSource.ProvideColumnGrandTotals = ShowColumnGrandTotals.Checked;
        dgCuboAdmPaciente.OptionsChartDataSource.ProvideRowGrandTotals = ShowRowGrandTotals.Checked;
        dgCuboAdmPaciente.OptionsChartDataSource.ProvideDataByColumns = ChartDataVertical.Checked;

    }

    protected void ChartType_ValueChanged(object sender, EventArgs e)
    {
        SetChartType(ChartType.SelectedItem.Text);
    }

    void SetChartType(string text)
    {
        WebChartControl1.SeriesTemplate.ChangeView((ViewType)Enum.Parse(typeof(ViewType), text));
        if (WebChartControl1.SeriesTemplate.Label != null)
        {
            PointLabels.Enabled = true;
            WebChartControl1.SeriesTemplate.Label.Visible = PointLabels.Checked;
        }
        else
            PointLabels.Enabled = false;
    }
    protected void PointLabels_CheckedChanged(object sender, EventArgs e)
    {
        WebChartControl1.SeriesTemplate.Label.Visible = PointLabels.Checked;
    }
    protected void ShowColumnGrandTotals_CheckedChanged(object sender, EventArgs e)
    {
        dgCuboAdmPaciente.OptionsChartDataSource.ProvideColumnGrandTotals = ShowColumnGrandTotals.Checked;
    }
    protected void ShowRowGrandTotals_CheckedChanged(object sender, EventArgs e)
    {
        dgCuboAdmPaciente.OptionsChartDataSource.ProvideRowGrandTotals = ShowRowGrandTotals.Checked;
    }
    protected void ChartDataVertical_CheckedChanged(object sender, EventArgs e)
    {
        dgCuboAdmPaciente.OptionsChartDataSource.ProvideDataByColumns = ChartDataVertical.Checked;
    }


    protected void btnGuardar_Click(object sender, EventArgs e)
    {
        CadenaReporte = dgCuboAdmPaciente.SaveLayoutToString();

        clsMar_Reportes_SegBL BL_Reporte_Seg = new clsMar_Reportes_SegBL();
        MAR_REPORTES_SEG ENT_Reporte_Seg = new MAR_REPORTES_SEG();
        ENT_Reporte_Seg.Nombre_Reporte = txtNomCampo.Text;
        ENT_Reporte_Seg.Cadena = CadenaReporte;
        if (BL_Reporte_Seg.Insertar(ENT_Reporte_Seg))
        {
            //Util.showmsg("Se Creo un Nuevio Reporte!!!", this.Page);
            lblMensaje.Text = "Se Creo un Nuevio Reporte!!!";
            ScriptManager.RegisterStartupScript(this, typeof(Page), "scriptkey", "pcMensaje.Show();", true);
        }
        else
        {
            //Util.showmsg("Error: No se pudo grabar reporte!!!", this.Page);
            lblMensaje.Text = "Error: No se pudo grabar reporte!!!";
            ScriptManager.RegisterStartupScript(this, typeof(Page), "scriptkey", "pcMensaje.Show();", true);
        }
    }
    protected void btnCargar_Click(object sender, EventArgs e)
    {
        clsMar_Reportes_SegBL BL_Reporte = new clsMar_Reportes_SegBL();
        var Consulta=from P in BL_Reporte.listar()
                     where P.idReporte==Convert.ToInt32(cboReportes.SelectedItem.Value)
                     select P;
                     foreach (var S in Consulta)
                     {
                         dgCuboAdmPaciente.LoadLayoutFromString(S.Cadena);
                     }

    }
    protected void ChartType_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}